package com.gome.han.beencloud.jdk.bio.echo.demo1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.gome.han.beencloud.Const;

/**
* @author hanpeng
* @version 创建时间：2020年10月27日 下午8:20:44
* @ClassName 类名称
* @Description 类描述
*/
public class BioServerHandler implements Runnable {
	private Socket socket;
	
	public BioServerHandler(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try (
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			PrintWriter printWriter=new PrintWriter(socket.getOutputStream(),true);
			
		) {
			String message;
			String result;
			while ((message=bufferedReader.readLine())!=null) {
				System.out.println("Server accept message: "+message);
				result=Const.response(message);
				printWriter.println(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(socket!=null) {
				try {
					socket.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				socket=null;
			}
		}
	}

}
